﻿using System;
using System.Collections.Generic;
using XNAFight.Scene.Core;
using Microsoft.Xna.Framework;
using XNAFight.Engine.Inputs;

namespace XNAFight.Scene.CharacterSelection
{
    class CharacterPicker : MultiMenuScreen
    {
        #region CHAMPS

        public List<List<string>> persos_team_1;
        public List<List<string>> persos_team_2;

        private bool selection_complete;

        #endregion

        #region PROPRIETES

        public bool SelectionComplete
        {
            get { return selection_complete; }
        }

        #endregion

        #region INITIALISATION

        public CharacterPicker(List<int> _nb_characters_team_1, List<int> _nb_characters_team_2)
            : base("")
        {
            default_selection_x = 1;
            default_selection_y = 0;

            items = new MenuItem[7, 3];

            items[1, 0] = new TextMenuItem(new Vector2(240, 150), "Ryu", 0.5f);
            items[2, 0] = new TextMenuItem(new Vector2(300, 150), "Guile", 0.5f);
            items[3, 0] = new TextMenuItem(new Vector2(360, 150), "Zangief", 0.5f);
            items[4, 0] = new TextMenuItem(new Vector2(420, 150), "Char 04", 0.5f);
            items[5, 0] = new TextMenuItem(new Vector2(480, 150), "Char 05", 0.5f);

            items[0, 1] = new TextMenuItem(new Vector2(180, 230), "Char 06", 0.5f);
            items[1, 1] = new TextMenuItem(new Vector2(240, 230), "Char 07", 0.5f);
            items[2, 1] = new TextMenuItem(new Vector2(300, 230), "Char 08", 0.5f);
            items[3, 1] = new TextMenuItem(new Vector2(360, 230), "Char 09", 0.5f);
            items[4, 1] = new TextMenuItem(new Vector2(420, 230), "Char 10", 0.5f);
            items[5, 1] = new TextMenuItem(new Vector2(480, 230), "Char 11", 0.5f);
            items[6, 1] = new TextMenuItem(new Vector2(540, 230), "Char 12", 0.5f);

            items[1, 2] = new TextMenuItem(new Vector2(240, 310), "Char 13", 0.5f);
            items[2, 2] = new TextMenuItem(new Vector2(300, 310), "Char 14", 0.5f);
            items[3, 2] = new TextMenuItem(new Vector2(360, 310), "Char 15", 0.5f);
            items[4, 2] = new TextMenuItem(new Vector2(420, 310), "Char 16", 0.5f);
            items[5, 2] = new TextMenuItem(new Vector2(480, 310), "Char 17", 0.5f);

            items[1, 0].SelectedBy += charSelected;
            items[2, 0].SelectedBy += charSelected;
            items[3, 0].SelectedBy += charSelected;
            /*items[4, 0].Selected += charSelected;
            items[5, 0].Selected += charSelected;
            items[0, 1].Selected += charSelected;
            items[1, 1].Selected += charSelected;
            items[2, 1].Selected += charSelected;
            items[3, 1].Selected += charSelected;
            items[4, 1].Selected += charSelected;
            items[5, 1].Selected += charSelected;
            items[6, 1].Selected += charSelected;
            items[1, 2].Selected += charSelected;
            items[2, 2].Selected += charSelected;
            items[3, 2].Selected += charSelected;
            items[4, 2].Selected += charSelected;
            items[5, 2].Selected += charSelected;*/

            this.Added += this.Activate;

            persos_team_1 = new List<List<string>>();
            persos_team_2 = new List<List<string>>();

            for (int i = 0; i < _nb_characters_team_1.Count; i++)
            {
                List<string> persos_player = new List<string>();
                for (int j = 0; j < _nb_characters_team_1[i]; j++)
                {
                    persos_player.Add("");
                }
                persos_team_1.Add(persos_player);
            }
            for (int i = 0; i < _nb_characters_team_2.Count; i++)
            {
                List<string> persos_player = new List<string>();
                for (int j = 0; j < _nb_characters_team_2[i]; j++)
                {
                    persos_player.Add("");
                }
                persos_team_2.Add(persos_player);
            }

            selection_complete = false;
        }

        #endregion

        #region OVERRIDE

        public override void Reset()
        {
            base.Reset();

            for (int i = 0; i < persos_team_1.Count; i++)
            {
                for (int j = 0; j < persos_team_1[i].Count; j++)
                {
                    persos_team_1[i][j] = "";
                }
            }
            for (int i = 0; i < persos_team_2.Count; i++)
            {
                for (int j = 0; j < persos_team_2[i].Count; j++)
                {
                    persos_team_2[i][j] = "";
                }
            }

            selection_complete = false;
        }

        #endregion

        #region METHODES

        private void charSelected(object _sender, EventArgs<ControlerSelection> _args)
        {
            List<List<string>> persos = persos_team_1;
            int index = _args.Value.PlayerID - 1;

            if (_args.Value.PlayerID - 1 >= persos_team_1.Count)
            {
                persos = persos_team_2;
                index = index - persos_team_1.Count;
            }

            int i = 0;
            while (i < persos[index].Count && persos[index][i] != "")
            {
                i++;
            }

            if (i < persos[index].Count)
                persos[index][i] = ((TextMenuItem)items[_args.Value.SelectionX, _args.Value.SelectionY]).Text;

            if (i == persos[index].Count - 1)
                _args.Value.Active = false;

            selection_complete = isSelectionComplete();
        }
        private bool isSelectionComplete()
        {
            bool is_complete = true;

            int i = 0;
            while (i < persos_team_1.Count && is_complete)
            {
                int j = 0;
                while (j < persos_team_1[i].Count && is_complete)
                {
                    if (persos_team_1[i][j] == "")
                        is_complete = false;
                    j++;
                }
                
                i++;
            }
            i = 0;
            while (i < persos_team_2.Count && is_complete)
            {
                int j = 0;
                while (j < persos_team_2[i].Count && is_complete)
                {
                    if (persos_team_2[i][j] == "")
                        is_complete = false;
                    j++;
                }

                i++;
            }

            return is_complete;
        }

        public void removeCharacterForPlayer(int _player_id)
        {
            List<List<string>> persos = persos_team_1;
            int index = _player_id - 1;

            if (index >= persos_team_1.Count)
            {
                persos = persos_team_2;
                index = index - persos_team_1.Count;
            }

            if (persos[index].Count == 0)
                return;

            int i = persos[index].Count - 1;
            while (i >= 0 && persos[index][i] == "")
            {
                i--;
            }

            if (i >= 0)
                persos[index][i] = "";

            getSelectionFromPlayerID(_player_id).Active = true;
            selection_complete = false;
        }
        public bool playerHasACharacter(int _player_id)
        {
            bool char_ok = false;

            List<List<string>> persos = persos_team_1;
            int index = _player_id - 1;

            if (index >= persos_team_1.Count)
            {
                persos = persos_team_2;
                index = index - persos_team_1.Count;
            }
            
            int i = 0;
            while (i < persos[index].Count && !char_ok)
            {
                if (persos[index][i] != "")
                    char_ok = true;
                i++;
            }

            return char_ok;
        }
        public int getPlayersNbCharacters(int _player_id)
        {
            int nb_char = 0;

            List<List<string>> persos = persos_team_1;
            int index = _player_id - 1;

            if (index >= persos_team_1.Count)
            {
                persos = persos_team_2;
                index = index - persos_team_1.Count;
            }

            int i = 0;
            while (i < persos[index].Count)
            {
                if (persos[index][i] != "")
                    nb_char++;
                i++;
            }

            return nb_char;
        }

        #endregion
    }
}
